<?php
/**
 * 用户管理类
 * comfiger
 * 2016-8-28
 **/
namespace Mama\Model;
use Think\Model;
class MemberModel extends Model{
	//定义表名称
	protected $tableName = 'member';
	
	/**
	 * 添加用户信息
	 **/
	public function registerMemberInfo($data){
		return $this->data($data)->add();
	}
	
	/**
	 * 编辑用户信息
	 **/
	public function editMemberInfo($userid, $data){
		return $this->where(array('userid' => $userid))->setField($data);
	}
	
	/**
	 * 根据登陆帐号获取信息
	 **/
	public function getMemberInfoByUsername($username){
		return $this->where(array('username' => $username))->find();
	}
	
	/**
	 * 根据三方登陆帐号登陆
	 **/
	public function getMemberInfoByOtherName($login_uuid){
		return $this->where(array('login_uuid' => $login_uuid))->find();
	}
	
	/**
	 * 根据用户ID获取用户详情
	 **/
	public function getMemberInfoByUserid($userid){
		return $this->where(array('userid' => $userid))->find();
	}

	/**
	 * 根据条件查询总数
	 **/
	public function getMemberCount($where){
		return $this->where($where)->count();
	}
	
	/**
	 * 获取用户列表
	 **/
	public function getMemberList($where, $page){
		return $this->where($where)
					->order('userid desc')
					->page($page, C('PAGE_SIZE'))
					->select();
	}
	
	/**
	 * 根据条件查询总数
	 **/
	public function getAdminMemberCount($where){
		return $this->alias('member')->where($where)->count();
	}
	
	/**
	 * 获取用户列表
	 **/
	public function getAdminMemberList($where, $page){
		return $this->alias('member')
					->join('left join mama_role as roles on member.roleid = roles.role_id')
					->where($where)
					->field('member.*, roles.role_name')
					->order('userid desc')
					->page($page, C('PAGE_SIZE'))
					->select();
	}

	/**
	 * 根据条件查询合同总数
	 **/
	public function getContractMemberCount($where){
		return M('member_profile')
					->alias('pros')
					->join('left join mama_member as member on pros.userid = member.userid')
					->join('left join mama_village as village on pros.villageid = village.villageid')
					->where($where)
					->count();
	}
	
	/**
	 * 获取用户合同列表
	 **/
	public function getContractMemberList($where, $page){
		return M('member_profile')->alias('pros')
					->join('left join mama_member as member on pros.userid = member.userid')
					->join('left join mama_village as village on pros.villageid = village.villageid')
					->field('pros.*, member.nickname, member.phone, member.sex,village.title')
					->where($where)
					->order('pros.profile_id desc')
					->page($page, C('PAGE_SIZE'))
					->select();
	}

	/**
	 * 获取合同详情
	 **/
	public function getContractMemberInfo($profile_id){
		return M('member_profile')->alias('pros')
					->join('left join mama_member as member on pros.userid = member.userid')
					->field('pros.*, member.nickname, member.phone, member.sex,member.card,member.address,member.emergency_name,member.emergency_phone')
					->where(array('pros.profile_id' => $profile_id))
					->find();
	}

	/**
	 * 编辑用户ID查询合同详情
	 **/
	public function editContractInfoByProfileId($profile_id, $data){
		return M('member_profile')->where(array(
			'profile_id' => $profile_id
		))->setField($data);
	}
}